package com.br.planningrun.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class ListaPlanoCorridaAdapter extends SimpleAdapter{
	
	SimpleDateFormat dataOut = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

	public ListaPlanoCorridaAdapter(Context context,
			List<? extends Map<String, ?>> data, int resource, String[] from,
			int[] to) {
		super(context, data, resource, from, to);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = super.getView(position, convertView, parent);
		
		// Recupera a linha
		HashMap<String, String> map = (HashMap<String, String>) super.getItem(position);				
		
		// Verifica se ja foi finalizada
		if (String.valueOf(map.get("codigoFinalizado")).equals("1")) {
			// Pinta fundo de verde
			if (map.get("statusPedido") != null && String.valueOf(map.get("codigoFinalizado")).equals("6")) {
				view.setBackgroundColor(Color.rgb(0, 0, 255));
			} else {
				view.setBackgroundColor(Color.rgb(50, 205, 50));	
			}
		} else if (String.valueOf(map.get("codigoFinalizado")).equals("6")) {
			view.setBackgroundColor(Color.rgb(0, 0, 255));
		} else {
			try {
				String dataAux = String.valueOf(map.get("data")) + " " + String.valueOf(map.get("hora"));
				Date dataPedido = dataOut.parse(dataAux);
				Date dataAtual = new Date();
				
				// Comparacao das datas
				if (dataPedido.before(dataAtual)) {
					// Pinta fundo de vermelho
					view.setBackgroundColor(Color.rgb(255, 0, 0));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return view;
	}
}
